<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3
 * Time: 16:48
 */

namespace app\admin\controller;

use app\model\CourierOrder as ModelCourierOrder;
use app\logic\Excel as MobileLogicExcel;
use app\model\UserOrder as ModelUserOrder;
use app\model\UserOrderLog as ModelUserOrderLog;


class Order extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 快递员订单
     * uri:/admin/order.courier
     */
    public function courierOrder(){
        $orderState = $this->_getContentParams('orderState');
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        if (empty($int_now_page)){
            $int_now_page = 1;
        }
        _toValidate('id', $int_now_page, 10005);

        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        if (empty($int_list_rows)){
            $int_list_rows = 10;
        }
        _toValidate('id', $int_list_rows, 10006);

        $arr_where = '';
        switch ($orderState){
            case 100:
                $arr_where.="courier_order.state in (20,25,30,40,70,75,80,90)";
                break;
            case 75:
                $arr_where.="courier_order.state=75";
                break;
            case 20:
                $arr_where.="courier_order.state in (20,70)";
                break;
            case 30:
                $arr_where.="courier_order.state in (25,30,75)";
                break;
            case 80:
                $arr_where.="courier_order.state in (40,80)";
                break;
            case 90:
                $arr_where.="courier_order.state=90";
                break;
            default:
                break;
        }
        $orderNumber = $this->_getContentParams('orderNumber');
        $userAccount = $this->_getContentParams('userAccount');
        $startTime = $this->_getContentParams('startTime');
        $endTime = $this->_getContentParams('endTime');
        if(!empty($orderNumber)){
            $arr_where.=" and (user_order.order_sn like '%$orderNumber%' or user_order.express_order_sn like '%$orderNumber%')";
        }

        if (!empty($userAccount)){
            $arr_where.=" and courier_info.mobile like '%$userAccount%'";
        }


        if (!empty($startTime)){
            $arr_where.=" and DATE_FORMAT(courier_order.gmt_create,'%Y%m%d') >= DATE_FORMAT('$startTime','%Y%m%d')";
        }

        if (!empty($endTime)){
            $arr_where.=" and DATE_FORMAT(courier_order.gmt_create,'%Y%m%d') <= DATE_FORMAT('$endTime','%Y%m%d')";
        }

        $int_total_rows = ModelCourierOrder::getInstance()->getToCount($arr_where,2);
        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询

        $list = ModelCourierOrder::getInstance()->getCourierOrderList($arr_where,'courier_order.id,courier_order.courier_id as courierId,courier_order.order_id as orderId,user_order.order_sn as orderSn,user_order.express_order_sn as expressOrderSn,courier_order.gmt_create as gmtCreate,user_order.order_amount as orderAmount,user_order.protect_price as protectPrice,courier_order.state,courier_info.mobile',$int_first_row,$int_list_rows,'2');
        _returnData(['totalPages' => $int_total_pages,'courierInfoList'=>$list]);
    }

    /**
     * 快递员订单数量
     * uri:/admin/order.courierNbr
     */
    public function courierOrderNbr(){

        $allOrder = ModelCourierOrder::getInstance()->getToCount("courier_order.state in (20,25,30,40,70,75,80,90)",2);
        $toSendOrder = ModelCourierOrder::getInstance()->getToCount("courier_order.state=75",2);
        $toTake = ModelCourierOrder::getInstance()->getToCount("courier_order.state in (20,70)",2);
        $haveToTake = ModelCourierOrder::getInstance()->getToCount("courier_order.state in (25,30,75)",2);
        $hasBeenCompleted = ModelCourierOrder::getInstance()->getToCount("courier_order.state in (40,80)",2);
        $hasBeenCancelled = ModelCourierOrder::getInstance()->getToCount("courier_order.state=90",2);
        _returnData(['allOrder'=>$allOrder,'toSendOrder'=>$toSendOrder,'toTake'=>$toTake,'haveToTake'=>$haveToTake,'hasBeenCompleted'=>$hasBeenCompleted,'hasBeenCancelled'=>$hasBeenCancelled]);
    }

    /**
     * 快递员订单列表导出
     * uri:/admin/order.courierExport
     */
    public function courierOrderExport(){
        $type = $this->_getContentParams('type');
        $str_excel = $this->_getContentParams('excel');
        $arr_where = '';
        if ($type==1){
            $orderState = $this->_getContentParams('orderState');
            switch ($orderState){
                case 100:
                    $arr_where.="courier_order.state in (20,30,40,70,75,80,90)";
                    break;
                case 75:
                    $arr_where.="courier_order.state=75";
                    break;
                case 20:
                    $arr_where.="courier_order.state in (20,70)";
                    break;
                case 30:
                    $arr_where.="courier_order.state in (30,75)";
                    break;
                case 80:
                    $arr_where.="courier_order.state in (40,80)";
                    break;
                case 90:
                    $arr_where.="courier_order.state=90";
                    break;
                default:
                    break;
            }
            $orderNumber = $this->_getContentParams('orderNumber');
            $userAccount = $this->_getContentParams('userAccount');
            $startTime = $this->_getContentParams('startTime');
            $endTime = $this->_getContentParams('endTime');
            if(!empty($orderNumber)){
                $arr_where.=" and (user_order.order_sn like '%$orderNumber%' or user_order.express_order_sn like '%$orderNumber%')";
            }

            if (!empty($userAccount)){
                $arr_where.=" and courier_info.mobile like '%$userAccount%'";
            }


            if (!empty($startTime)){
                $arr_where.=" and DATE_FORMAT(courier_order.gmt_create,'%Y%m%d') >= DATE_FORMAT('$startTime','%Y%m%d')";
            }

            if (!empty($endTime)){
                $arr_where.=" and DATE_FORMAT(courier_order.gmt_create,'%Y%m%d') <= DATE_FORMAT('$endTime','%Y%m%d')";
            }


        }elseif ($type==2){
            $arrOrderId = $this->_getContentParams('arrOrderId');

            $arr_where.="courier_order.id in ($arrOrderId)";
        }
        $list = ModelCourierOrder::getInstance()->getCourierOrderList($arr_where,'courier_order.courier_id as courierId,courier_order.order_id as orderId,user_order.order_sn as orderSn,user_order.express_order_sn as expressOrderSn,courier_order.gmt_create as gmtCreate,user_order.order_amount as orderAmount,user_order.protect_price as protectPrice,courier_order.state,courier_info.mobile','','','2');
        if($str_excel == 'excel'){
            MobileLogicExcel::getInstance()->export($list, ['courierId' => '快递员ID', 'orderId' => '订单ID', 'orderSn' => '订单编号', 'expressOrderSn' => '运单号','gmtCreate'=>'接单时间','mobile'=>'快递员账号','orderAmount'=>'订单金额','protectPrice'=>'保价金额','state'=>'订单状态(75：待派件 70、20：待取件 75、30：已取件 80、40：已完成 90：已取消)','describe'=>'状态信息'], '快递员订单'.date('Y-m-d', time()));
        }
        _returnData();
    }

    /**
     * 会员订单列表
     * uri:/admin/order.userList
     */
    public function orderUserList(){
        $orderState = $this->_getContentParams('orderState');
        _toCompare(empty($orderState),"==",true,20007);
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        if (empty($int_now_page)){
            $int_now_page = 1;
        }
        _toValidate('id', $int_now_page, 10005);

        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        if (empty($int_list_rows)){
            $int_list_rows = 10;
        }
        _toValidate('id', $int_list_rows, 10006);

        $arr_where = '';
        switch ($orderState){
            case 100://全部
                $arr_where.="user_order.order_state in (10,20,25,80,0)";
                break;
            case 10://待支付
                $arr_where.="user_order.order_state in (10,20,25)";
                break;
            case 20://待取件
                $arr_where.="user_order.order_state=20";
                break;
            case 80://已完成
                $arr_where.="user_order.order_state=80";
                break;
            case 0://已取消
                $arr_where.="user_order.order_state=0";
                break;
            default:
                break;
        }
        $orderNumber = $this->_getContentParams('orderNumber');
        $userAccount = $this->_getContentParams('userAccount');
        $startTime = $this->_getContentParams('startTime');
        $endTime = $this->_getContentParams('endTime');
        if(!empty($orderNumber)){
            $arr_where.=" and (user_order.order_sn like '%$orderNumber%' or user_order.express_order_sn like '%$orderNumber%')";
        }

        if (!empty($userAccount)){
            $arr_where.=" and courier_info.mobile like '%$userAccount%'";
        }


        if (!empty($startTime)){
            $arr_where.=" and DATE_FORMAT(courier_order.gmt_create,'%Y%m%d') >= DATE_FORMAT('$startTime','%Y%m%d')";
        }

        if (!empty($endTime)){
            $arr_where.=" and DATE_FORMAT(courier_order.gmt_create,'%Y%m%d') <= DATE_FORMAT('$endTime','%Y%m%d')";
        }

        $int_total_rows = ModelCourierOrder::getInstance()->getToCount($arr_where,2);
        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询

        $list = ModelUserOrder::getInstance()->getUserOrderList($arr_where,$int_first_row,$int_list_rows);
        _returnData(['totalPages' => $int_total_pages,'userInfoList'=>$list]);
    }

    /**
     * 会员订单数量
     * uri:/admin/order.userNbr
     */
    public function userOrderNbr(){

        $allOrder = ModelUserOrder::getInstance()->toCount("user_order.order_state in (10,20,25,80,0)");
        $toBePaid = ModelUserOrder::getInstance()->toCount("user_order.order_state in (10,20,25)");
        $toTake = ModelUserOrder::getInstance()->toCount("user_order.order_state=20");
        $hasBeenCompleted = ModelUserOrder::getInstance()->toCount("user_order.order_state=80");
        $hasBeenCancelled = ModelUserOrder::getInstance()->toCount("user_order.order_state=0");
        _returnData(['allOrder'=>$allOrder,'toBePaid'=>$toBePaid,'toTake'=>$toTake,'hasBeenCompleted'=>$hasBeenCompleted,'hasBeenCancelled'=>$hasBeenCancelled]);
    }

    /**
     * 会员订单导出
     * uri:/admin/order.userExport
     */
    public function orderUserExport(){
        $type = $this->_getContentParams('type');
        $str_excel = $this->_getContentParams('excel');
        $arr_where = '';
        if ($type==1){
            $orderState = $this->_getContentParams('orderState');
            switch ($orderState){
                case 100://全部
                    $arr_where.="user_order.order_state in (10,20,25,80,0)";
                    break;
                case 10://待支付
                    $arr_where.="user_order.order_state in (10,20,25)";
                    break;
                case 20://待取件
                    $arr_where.="user_order.order_state=20";
                    break;
                case 80://已完成
                    $arr_where.="user_order.order_state=80";
                    break;
                case 0://已取消
                    $arr_where.="user_order.order_state=0";
                    break;
                default:
                    break;
            }
            $orderNumber = $this->_getContentParams('orderNumber');
            $userAccount = $this->_getContentParams('userAccount');
            $startTime = $this->_getContentParams('startTime');
            $endTime = $this->_getContentParams('endTime');
            if(!empty($orderNumber)){
                $arr_where.=" and (user_order.order_sn like '%$orderNumber%' or user_order.express_order_sn like '%$orderNumber%')";
            }

            if (!empty($userAccount)){
                $arr_where.=" and courier_info.mobile like '%$userAccount%'";
            }


            if (!empty($startTime)){
                $arr_where.=" and DATE_FORMAT(courier_order.gmt_create,'%Y%m%d') >= DATE_FORMAT('$startTime','%Y%m%d')";
            }

            if (!empty($endTime)){
                $arr_where.=" and DATE_FORMAT(courier_order.gmt_create,'%Y%m%d') <= DATE_FORMAT('$endTime','%Y%m%d')";
            }
        }elseif ($type==2){
            $arrOrderId = $this->_getContentParams('arrOrderId');
            $arr_where.="user_order.order_id in ($arrOrderId)";
        }
        $list = ModelUserOrder::getInstance()->getUserOrderList($arr_where,'','');
        if($str_excel == 'excel'){
            MobileLogicExcel::getInstance()->export($list, ['orderId' => '订单ID', 'orderSn' => '订单编号', 'expressOrderSn' => '运单号','gmtCreate'=>'下单时间','mobile'=>'会员账号','orderAmount'=>'订单金额','paytype'=>'支付方式（0：无，100：支付宝支付，200：微信支付）','orderState'=>'订单状态(100：全部订单 80：已完成 20：待取件 10：待支付 0：已取消)','describe'=>'状态信息'], '会员订单'.date('Y-m-d', time()));
        }
        _returnData();
    }

    /**
     * 订单备注
     * uri:/admin/order.remark
     */
    public function orderRemark(){
        $orderId = $this->_getContentParams('orderId');
        _toValidate('id',$orderId,20013);
        $remark = $this->_getContentParams('remark');
        $data = ModelUserOrderLog::getInstance()->addOrderLog($orderId,5,$this->admin_id,$remark);
        if ($data>0){
            _returnData();
        }
    }
}